﻿using MassTransit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Orders.Application.Data;
using Orders.Worker.Consumers;


namespace Orders.Worker
{
    public class Program
    {
        public static async Task Main(string[] args)
        {

        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            .ConfigureServices((hostContext, services) =>
            {
                services.AddDbContext<OrdersDbContext>(x =>
                {
                    x.UseMySql(hostContext.Configuration.GetConnectionString("MyStoreDb"), new MySqlServerVersion(new Version(8, 4, 5)), Opt =>
                    {
                        Opt.EnableRetryOnFailure(5);
                    });
                });

                services.AddOptions<RabbitMqTransportOptions>().BindConfiguration("RabbitMq");

                services.AddMassTransit(x =>
                {
                    //var entryAssembly = Assembly.GetEntryAssembly();
                    //x.AddConsumers(entryAssembly);

                    x.AddConsumer<OrderCreatedConsumer>();

                    x.AddEntityFrameworkOutbox<OrdersDbContext>(o =>
                    {
                        o.QueryDelay = TimeSpan.FromSeconds(5);
                        o.UseMySql();
                    });

                    x.UsingRabbitMq((context, cfg) =>
                    {
                        cfg.ConfigureEndpoints(context);
                    });
                });
            });
    }
}
